/**
 * <pre>
 *     author : XML
 *     e-mail : 946891360@qq.com
 *     time   : 2019/07/04
 *     version:
 *     desc   :
 * </pre>
 */
import React, {Component} from 'react'
import {Text, Button, Image, TextInput, Platform, View, StyleSheet, BackHandler} from 'react-native'
import {NavigationActions} from "react-navigation";
import NavigationUtil from "../navigator/NavigationUtil";


export default class BackPressComponent extends Component {
    constructor(props) {
        super(props);
        NavigationUtil.navigation = this.props.navigation;
    }


    componentDidMount() {
        BackHandler.addEventListener('hardwareBackPress', this.onBackPress);
    }

    componentWillUnmount() {
        BackHandler.removeEventListener('hardwareBackPress', this.onBackPress)
    }

    /**
     * 处理 Android 中物理返回键
     */
    onBackPress = () => {
        const {dispatch, nav} = this.props;
        if (nav.routes[1].index === 0) {//如果RootNavigator中的nav.router[1]代表MainNavigator的index为0,则不处理返回事件
            return false;
        }
        dispatch(NavigationActions.back());
        return true;
    };
}

const styles = StyleSheet.create({
    container: {
        flex: 1
    }
});
